package com.shop.controller.admin;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TreeMap;

import com.jf.core.BaseController;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.shop.bean.POrderBean;
import com.shop.utils.StringUtils;

public class OrderController extends BaseController{
	/**
	 * 点击未付款订单链接
	 * **/
	public void unapplyOrder(){
		String select = "select tblp.phyprice,tblop.psum,tblpo.ocode,tblpo.otime,tblpo.oprice,tblpo.odeal,tblpo.odeal1,tblpo.odeal2,tblpo.odeal3,tblpo.odeal4,tblp.pname,tblp.pphoto,tblp.pcode,tbladdr.name,tblzf.zname";
		String sqlExceptSelect = "from tblshrmess tblsm inner join tblporder tblpo on tblsm.ocode= tblpo.ocode and tblpo.odeal='1' and tblpo.odeal1='0'"+
		                         "inner join tblzhifu tblzf on tblzf.zcode=tblsm.zffs "+
				                 "inner join tbladdress tbladdr on tbladdr.adcode=tblsm.adcode "+
		                         "inner join tblorderproduct tblop on tblop.ocode=tblpo.ocode "+
		                         "inner join tblproduct tblp on tblp.pcode=tblop.pcode order by tblpo.ocode,tblpo.otime desc";
		Integer page = getParaToInt("page");
		if(page == null || "".equals(page)){
			page= 1;
		}
		Page<Record> myreviews = Db.paginate(page, 55, select,sqlExceptSelect);
		setAttr("myreviewPage",myreviews);
		List<Record> reviewList = myreviews.getList();
		TreeMap<String,List<Record>> reviewMap = new TreeMap<String,List<Record>>();
		for(Record review : reviewList){
			String ocode = review.getStr("ocode");
			List<Record> temp = reviewMap.get(ocode);
			if(temp == null){
				temp = new ArrayList<Record>();
				temp.add(review);
				reviewMap.put(ocode, temp);
			}else{
				temp.add(review);
			}
		}
		setAttr("myreviews", reviewMap.descendingMap());
		render(WEB_INF_URL+"admin/files/listunapplyorder.jsp");
	}
	
	/**
	 * 已付款的订单链接
	 * */
	public void applyOrder(){
		String select = "select tblp.phyprice,tblop.psum,tblpo.ocode,tblpo.time1,tblpo.otime,tblpo.oprice,tblpo.odeal,tblpo.odeal1,tblpo.odeal2,tblpo.odeal3,tblpo.odeal4,tblp.pname,tblp.pphoto,tblp.pcode,tbladdr.name,tbladdr.tel,tblzf.zname";
		String sqlExceptSelect = "from tblshrmess tblsm inner join tblporder tblpo on tblsm.ocode= tblpo.ocode and tblpo.odeal1='1' and tblpo.odeal2='0'"+
		                         "inner join tblzhifu tblzf on tblzf.zcode=tblsm.zffs "+
				                 "inner join tbladdress tbladdr on tbladdr.adcode=tblsm.adcode "+
		                         "inner join tblorderproduct tblop on tblop.ocode=tblpo.ocode "+
		                         "inner join tblproduct tblp on tblp.pcode=tblop.pcode order by tblpo.ocode,tblpo.otime desc";
		Integer page = getParaToInt("page");
		if(page == null || "".equals(page)){
			page= 1;
		}
		Page<Record> myreviews = Db.paginate(page, 55, select,sqlExceptSelect);
		setAttr("myreviewPage",myreviews);
		List<Record> reviewList = myreviews.getList();
		TreeMap<String,List<Record>> reviewMap = new TreeMap<String,List<Record>>();
		for(Record review : reviewList){
			String ocode = review.getStr("ocode");
			List<Record> temp = reviewMap.get(ocode);
			if(temp == null){
				temp = new ArrayList<Record>();
				temp.add(review);
				reviewMap.put(ocode, temp);
			}else{
				temp.add(review);
			}
		}
		setAttr("myreviews", reviewMap.descendingMap());
		render(WEB_INF_URL+"admin/files/listapplyorder.jsp");
	}
	
	/**
	 * 点击确认付款操作（商家确认收款）
	 * */
	public void commitPay(){
		String ocode = getPara("ocode");
		boolean bool = POrderBean.poDao.findById(ocode)
				       .set("odeal2", "1")
				       .set("time2",StringUtils.getFormatTime(new Date()))
				       .update();
		if(bool){
			renderJson("message","true");
		}else{
			renderJson("message","false");
		}
	}
	
	/**
	 * 等待发货的商品订单列表
	 * */
	public void waitshipOrder(){
		String select = "select tblp.phyprice,tblop.psum,tblpo.ocode,tblpo.otime,tblpo.oprice,tblpo.odeal,tblpo.shouhuo,tblpo.odeal1,tblpo.odeal2,tblpo.odeal3,tblpo.odeal4,tblp.pname,tblp.pphoto,tblp.pcode,tbladdr.name,tbladdr.addr,tblzf.zname";
		String sqlExceptSelect = "from tblshrmess tblsm inner join tblporder tblpo on tblsm.ocode= tblpo.ocode and tblpo.odeal2='1' and tblpo.odeal3='0'"+
		                         "inner join tblzhifu tblzf on tblzf.zcode=tblsm.zffs "+
				                 "inner join tbladdress tbladdr on tbladdr.adcode=tblsm.adcode "+
		                         "inner join tblorderproduct tblop on tblop.ocode=tblpo.ocode "+
		                         "inner join tblproduct tblp on tblp.pcode=tblop.pcode order by tblpo.ocode,tblpo.otime desc";
		Integer page = getParaToInt("page");
		if(page == null || "".equals(page)){
			page= 1;
		}
		Page<Record> myreviews = Db.paginate(page, 55, select,sqlExceptSelect);
		setAttr("myreviewPage",myreviews);
		List<Record> reviewList = myreviews.getList();
		TreeMap<String,List<Record>> reviewMap = new TreeMap<String,List<Record>>();
		for(Record review : reviewList){
			String ocode = review.getStr("ocode");
			List<Record> temp = reviewMap.get(ocode);
			if(temp == null){
				temp = new ArrayList<Record>();
				temp.add(review);
				reviewMap.put(ocode, temp);
			}else{
				temp.add(review);
			}
		}
		setAttr("myreviews", reviewMap.descendingMap());
		render(WEB_INF_URL+"admin/files/waitshiporder.jsp");
	}
	
	/**
	 * 点击发货操作（商家发货）
	 * */
	public void ship(){
		String ocode = getPara("ocode");
		boolean bool = POrderBean.poDao.findById(ocode)
				       .set("odeal3", "1")
				       .set("time3",StringUtils.getFormatTime(new Date()))
				       .update();
		if(bool){
			renderJson("message","true");
		}else{
			renderJson("message","false");
		}
	}
	
	/**
	 * 已发货的商品订单列表
	 * */
	public void shippedOrder(){
		String select = "select tblp.phyprice,tblop.psum,tblpo.ocode,tblpo.otime,tblpo.time3,tblpo.oprice,tblpo.odeal,tblpo.odeal1,tblpo.odeal2,tblpo.odeal3,tblpo.odeal4,tblp.pname,tblp.pphoto,tblp.pcode,tbladdr.name,tbladdr.addr,tblzf.zname";
		String sqlExceptSelect = "from tblshrmess tblsm inner join tblporder tblpo on tblsm.ocode= tblpo.ocode and tblpo.odeal3='1' and tblpo.odeal4='0'"+
		                         "inner join tblzhifu tblzf on tblzf.zcode=tblsm.zffs "+
				                 "inner join tbladdress tbladdr on tbladdr.adcode=tblsm.adcode "+
		                         "inner join tblorderproduct tblop on tblop.ocode=tblpo.ocode "+
		                         "inner join tblproduct tblp on tblp.pcode=tblop.pcode order by tblpo.ocode,tblpo.otime desc";
		Integer page = getParaToInt("page");
		if(page == null || "".equals(page)){
			page= 1;
		}
		Page<Record> myreviews = Db.paginate(page, 55, select,sqlExceptSelect);
		setAttr("myreviewPage",myreviews);
		List<Record> reviewList = myreviews.getList();
		TreeMap<String,List<Record>> reviewMap = new TreeMap<String,List<Record>>();
		for(Record review : reviewList){
			String ocode = review.getStr("ocode");
			List<Record> temp = reviewMap.get(ocode);
			if(temp == null){
				temp = new ArrayList<Record>();
				temp.add(review);
				reviewMap.put(ocode, temp);
			}else{
				temp.add(review);
			}
		}
		setAttr("myreviews", reviewMap.descendingMap());
		render(WEB_INF_URL+"admin/files/shippedorder.jsp");
	}
	
	/**
	 * 确认收货（商家手动操作用户已收货）
	 * */
	public void commitRec(){
		String ocode = getPara("ocode");
		boolean bool = POrderBean.poDao.findById(ocode)
				       .set("odeal4", "1")
				       .set("time4",StringUtils.getFormatTime(new Date()))
				       .update();
		if(bool){
			renderJson("message","true");
		}else{
			renderJson("message","false");
		}
	}
	
	/**
	 * 已完成的商品订单列表
	 * */
	public void completeOrder(){
		String select = "select tblp.phyprice,tblop.psum,tblpo.ocode,tblpo.otime,tblpo.time4,tblpo.oprice,tblpo.odeal,tblpo.odeal1,tblpo.odeal2,tblpo.odeal3,tblpo.odeal4,tblp.pname,tblp.pphoto,tblp.pcode,tbladdr.name,tbladdr.addr,tblzf.zname";
		String sqlExceptSelect = "from tblshrmess tblsm inner join tblporder tblpo on tblsm.ocode= tblpo.ocode and tblpo.odeal4='1'"+
		                         "inner join tblzhifu tblzf on tblzf.zcode=tblsm.zffs "+
				                 "inner join tbladdress tbladdr on tbladdr.adcode=tblsm.adcode "+
		                         "inner join tblorderproduct tblop on tblop.ocode=tblpo.ocode "+
		                         "inner join tblproduct tblp on tblp.pcode=tblop.pcode order by tblpo.ocode,tblpo.otime desc";
		Integer page = getParaToInt("page");
		if(page == null || "".equals(page)){
			page= 1;
		}
		Page<Record> myreviews = Db.paginate(page, 55, select,sqlExceptSelect);
		setAttr("myreviewPage",myreviews);
		List<Record> reviewList = myreviews.getList();
		TreeMap<String,List<Record>> reviewMap = new TreeMap<String,List<Record>>();
		for(Record review : reviewList){
			String ocode = review.getStr("ocode");
			List<Record> temp = reviewMap.get(ocode);
			if(temp == null){
				temp = new ArrayList<Record>();
				temp.add(review);
				reviewMap.put(ocode, temp);
			}else{
				temp.add(review);
			}
		}
		setAttr("myreviews", reviewMap.descendingMap());
		render(WEB_INF_URL+"admin/files/completeorder.jsp");
	}
}
